package com.hunmengwl.oauth.service.impl;

import com.hunmengwl.oauth.dto.LockDTO;
import com.hunmengwl.oauth.entity.LockEntity;
import com.hunmengwl.oauth.entity.ResultEntity;
import com.hunmengwl.oauth.repository.LockRepository;
import com.hunmengwl.oauth.repository.ResultRepository;
import com.hunmengwl.oauth.service.ILockService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author hunmeng
 */
@Service
@Transactional(readOnly = true)
public class LockServiceImpl implements ILockService {

    @Autowired
    private LockRepository lockRepository;

    @Autowired
    private ResultRepository resultRepository;

    @Override
    public String oauthLock(String value) {
        List<LockEntity> byValue = lockRepository.findByValue(value);
        ResultEntity resultEntity = resultRepository.findAll().get(0);
        if (byValue == null || byValue.size() <= 0) {
            return resultEntity.getFail();
        }
        return resultEntity.getSucceed();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<LockEntity> adds(LockDTO lockDTO)  {
        if(lockDTO.getNumber() == null){
            throw new RuntimeException("请输入正确的数量！！");
        }
        List<LockEntity> inserts = new ArrayList<>();
        for (Integer i = 0; i < lockDTO.getNumber(); i++) {
            LockEntity lockEntity = new LockEntity();
            lockEntity.setValue(UUID.randomUUID().toString().replace("-",""));
            lockEntity.setName(lockDTO.getName());
            lockEntity.setRemark(lockDTO.getRemark());
            inserts.add(lockEntity);
        }
        return lockRepository.saveAll(inserts);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importLock(LockDTO lockDTO) {
        if(StringUtils.isEmpty(lockDTO.getText())){
            throw new RuntimeException("请导入正确的卡密");
        }
        String[] split = lockDTO.getText().split("\\n");
        List<LockEntity> inserts = new ArrayList<>();
        for (String value : split) {
            LockEntity lockEntity = new LockEntity();
            lockEntity.setValue(value);
            lockEntity.setName(lockDTO.getName());
            lockEntity.setRemark(lockDTO.getRemark());
            inserts.add(lockEntity);
        }
        lockRepository.saveAll(inserts);
    }

    @Override
    public Page<LockEntity> findByPage(LockEntity lockEntity, Pageable pageable) {

        //查询条件构造
        Specification<LockEntity> spec = (Specification<LockEntity>) (root, query, cb) -> {
            if(StringUtils.isNotEmpty(lockEntity.getValue())){
                Predicate p1 = cb.like(root.get("value"), "%" + lockEntity.getValue() + "%");
                return p1;
            }
            return null;
        };
        return lockRepository.findAll(spec, pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String value) {
        if (StringUtils.isEmpty(value)){
            throw new RuntimeException("请输入正确的KEY");
        }
        List<LockEntity> byValue = lockRepository.findByValue(value);
        if(byValue.size() > 0){
            lockRepository.delete(byValue.get(0));
        }
    }

}
